/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Martin
 */
import javax.swing.JPanel;
import java.awt.Graphics;
import java.lang.String;
import java.awt.Color;
import java.awt.Font;

public class Picture extends JPanel {

    String type = "black";
    private double zoom = 10000;
    private double z = 1000;
    private double apertureWidth = 2;
    private double apertureHeight = 2;
    private double lambda = 530;
    private double exposition = 20.0;
    private double apertureDiameter = 100000;
    private boolean graph = false;

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        int height, width;

        height = this.getHeight();
        width = this.getWidth();

        if (type.equals("black")) {
            g.setColor(new Color(0, 0, 0));
            g.fillRect(0, 0, width, height);

        } else if (type.equals("circle")) {

            Circle rs = new Circle();
            double intensity;
            int colIntens;
            int y, x;

            rs.setA(getApertureDiameter()*0.000000001);
            rs.setExposition(getExposition());
            rs.setLambda(getLambda() * 0.000000001);
            rs.setZ(0.001 * getZ());
            rs.setZoom(1 / getZoom());

            for (y = 0; y < height; y++) {
                for (x = 0; x < width; x++) {

                    if (graph) {
                    } else {

                        for (y = 0; y < height; y++) {
                            for (x = 0; x < width; x++) {

                                intensity = rs.getIntensity(x - width / 2, y - height / 2);
                                if (intensity >= 1.0) {
                                    intensity = 1.0;
                                } else if (intensity < 0) {
                                    intensity = 0.0;
                                }

                                g.setColor(ColorLight(getLambda(),intensity));
                                g.drawLine(x, y, x, y);

                            }

                        }
                    }

                }
            }
        } else if (type.equals("rectangle")) {

            Rectangle r = new Rectangle(getLambda(), getExposition(),apertureWidth, getApertureHeight(), getZ());

            if (graph) {
            } else {

                int x0 = (int) (width / 2);
                int y0 = (int) (height / 2);

                for (int y = 0; y < y0; y++) {               // Screen Y loop
                    for (int x = 0; x < x0; x++) {             // Screen X loop

                        Color c;
                        if (x == 0 && y == 0) {
                            c = ColorLight(getLambda(), 1.0);
                        } else if (x == 0) {
                            c = ColorLight( getLambda(), r.getIntensity(x + 1, y));
                        } else if (y == 0) {
                            c = ColorLight( getLambda(), r.getIntensity(x, y + 1));
                        } else {
                            c = ColorLight( getLambda(), r.getIntensity(x, y));
                        }

                        g.setColor(c);
                        g.drawLine(x0 + x, y0 + y, x0 + x, y0 + y);      // Plot pattern
                        g.drawLine(x0 + x, y0 - y, x0 + x, y0 - y);      // Plot pattern
                        g.drawLine(x0 - x, y0 + y, x0 - x, y0 + y);      // Plot pattern
                        g.drawLine(x0 - x, y0 - y, x0 - x, y0 - y);      // Plot pattern
                    }
                }
            }



        } else if (type.equals("diagram")) {
        } else if (type.equals("formulas")) {
        }
    }

    private Color ColorLight(double wl, double intesity) {
        Color c;
        double i = intesity * 255;

        if (wl >= 380 && wl <= 440) {
            c = new Color((int) (-1 * (wl - 440) / (440 - 380) * i), 0, (int) i);
        } else if (wl > 440 && wl <= 490) {
            c = new Color(0, (int) ((wl - 440) / (490 - 440) * i), (int) i);
        } else if (wl > 490 && wl <= 510) {
            c = new Color(0, (int) i, (int) ((-1 * (wl - 510)) / (510 - 490) * i));
        } else if (wl > 510 && wl <= 580) {
            c = new Color((int) ((wl - 510) / (580 - 510) * i), (int) i, 0);
        } else if (wl > 580 && wl <= 645) {
            c = new Color((int) i, (int) ((-1 * (wl - 645)) / (645 - 580) * i), 0);
        } else {
            c = new Color((int) i, 0, 0);
        }

        return c;
    }

    public void setType(String type) {
        this.type = type;
    }

    public void setApertureDiameter(double apertureDiameter) {
        this.apertureDiameter = apertureDiameter;
    }

    public void setApertureHeight(double apertureHeight) {
        this.apertureHeight = apertureHeight;
    }

    public void setApertureWidth(double apertureWidth) {
        this.apertureWidth = apertureWidth;
    }

    public void setExposition(double exposition) {
        this.exposition = exposition;
    }

    public void setLambda(double lambda) {
        this.lambda = lambda;
    }

    public void setZ(double z) {
        this.z = z;
    }

    public void setZoom(double zoom) {
        this.zoom = zoom;
    }

    public void setGraph(boolean graph) {
        this.graph = graph;
    }

    /**
     * @return the zoom
     */
    public double getZoom() {
        return zoom;
    }

    /**
     * @return the z
     */
    public double getZ() {
        return z;
    }

    /**
     * @return the apertureHeight
     */
    public double getApertureHeight() {
        return apertureHeight;
    }

    /**
     * @return the lambda
     */
    public double getLambda() {
        return lambda;
    }

    /**
     * @return the exposition
     */
    public double getExposition() {
        return exposition;
    }

    /**
     * @return the apertureDiameter
     */
    public double getApertureDiameter() {
        return apertureDiameter;
    }
}
